package com.atguigu.gmall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/9/4 15:19
 * @Version 1.0
 */
@Configuration // 起到初始化作用，在管控台中初始化
public class DeadLetterMqConfig {

    // 声明一些变量
    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";

    //  创建交换机
    @Bean
    public DirectExchange deadExchange(){
        //  创建交换机
        return new DirectExchange(exchange_dead,true,false);
    }

    //  创建队列1
    @Bean
    public Queue deadQueue1(){
        //  创建map 集合
        HashMap<String, Object> map = new HashMap<>();
        //  设置过期时间 10s
        map.put("x-message-ttl",10000);
        //  指定交换机与队列
        map.put("x-dead-letter-exchange",exchange_dead);
        map.put("x-dead-letter-routing-key",routing_dead_2);
        //  返回队列
        return new Queue(queue_dead_1,true,false,false,map);
    }

    //  设置绑定1
    @Bean
    public Binding binding1(){
        //  设置绑定关系！
        return BindingBuilder.bind(deadQueue1()).to(deadExchange()).with(routing_dead_1);
    }


    //  创建队列2
    @Bean
    public Queue deadQueue2(){
        //  返回队列
        return new Queue(queue_dead_2,true,false,false);
    }

    //  设置绑定2
    @Bean
    public Binding binding2(){
        //  设置绑定关系！
        return BindingBuilder.bind(deadQueue2()).to(deadExchange()).with(routing_dead_2);
    }



}
